package com.sj.szxy.courseschedule.entity;

import java.time.LocalTime;

import com.sj.szxy.courseschedule.constrant.CompoundConstraint;
import com.sj.szxy.courseschedule.constrant.Constraint;
import com.sj.szxy.courseschedule.entity.course.Course;

/**
 * 排课的时间段
 * @author Yi Ping
 * @date 2018年3月29日 上午10:52:29 
 * @since 1.0.0
 *
 */
public class Segment  implements Comparable<Segment>{
	private static final String[] weeknames = new String[] {"星期一","星期二", "星期三 ", "星期四", "星期五","星期六","星期日"};
	
	private int weekday;	// 星期
	private int ordinal;	// 第几节课
	private Noon noon;		//时间片是上午,下午,还是晚上
	private LocalTime start;
	private LocalTime end;
	
	/** 与该时间片紧邻的两个两个时间片 **/
	private Segment last;
	private	Segment next;
	
	private transient CompoundConstraint constraint = new CompoundConstraint();
	
	
	
	/**
	 * @param weekday
	 * @param ordinal
	 * @param noon
	 */
	public Segment(int weekday, int ordinal, Noon noon) {
		this.weekday = weekday;
		this.ordinal = ordinal;
		this.noon = noon;
	}
	
	public void addConstrant(Constraint contraint) {
		constraint.add(contraint);
	}
	
	
	public boolean support(Course course) {
		return isFree() && constraint.support(course);
	}
	
	/**
	 *  该时间片是否空闲，即是否可用于排课
	 * @return
	 */
	public boolean isFree() {
		return true;
	}
	
	
	public Segment last() {
		return this.last;
	}
	
	public Segment next() {
		return this.next;
	}
	
	
	public int getWeekday() {
		return weekday;
	}



	public void setWeekday(int weekday) {
		this.weekday = weekday;
	}



	public int getOrdinal() {
		return ordinal;
	}



	public void setOrdinal(int ordinal) {
		this.ordinal = ordinal;
	}

	
	
	public Noon getNoon() {
		return noon;
	}

	public void setNoon(Noon noon) {
		this.noon = noon;
	}

	public LocalTime getStart() {
		return start;
	}

	public void setStart(LocalTime start) {
		this.start = start;
	}

	public LocalTime getEnd() {
		return end;
	}

	public void setEnd(LocalTime end) {
		this.end = end;
	}

	public Segment getLast() {
		return last;
	}

	public void setLast(Segment last) {
		this.last = last;
	}

	public Segment getNext() {
		return next;
	}

	public void setNext(Segment next) {
		this.next = next;
	}

	public CompoundConstraint getConstraint() {
		return constraint;
	}

	public void setConstraint(CompoundConstraint constraint) {
		this.constraint = constraint;
	}
	
	

	public static String[] getWeeknames() {
		return weeknames;
	}

	public boolean equals(Segment other) {
		if(other==null) return false;
		return this.weekday == other.weekday && this.ordinal == other.ordinal;
	}

	@Override
	public String toString() {
		return weeknames[weekday] + "第" + ordinal + "节课";
	}

	@Override
	public int compareTo(Segment other) {
		if(this.weekday == other.weekday)
			return this.ordinal - other.ordinal;
		else
			return this.weekday - other.weekday;
	}
	
	
	
	
}
